package com.eastfair.venuebooking.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.api.PaymentPlanFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.pay.dto.PaymentPlanDTO;
import com.eastfair.pay.dto.PaymentPlanPageQuery;
import com.eastfair.pay.vo.PaymentPlanVO;
import com.eastfair.venuebooking.entity.ContractManage;
import com.eastfair.venuebooking.service.ContractManageService;
import com.eastfair.venuebooking.service.VenueBookingPaymentPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 付款计划 Service.
 *
 * @author dqq
 * @date 2022 /6/24
 */
@Service
@Slf4j
public class VenueBookingPaymentPlanServiceImpl implements VenueBookingPaymentPlanService {

    @Resource
    private PaymentPlanFeign paymentPlanFeign;

    @Resource
    private ContractManageService contractManageService;

    @Override
    public List<PaymentPlanVO> queryByContractId(Long contractId) {
        log.info("selectPaymentPlanListByContractId - 合同id查询支付计划列表, contractId={}", contractId);
        R<List<PaymentPlanVO>> result = paymentPlanFeign.queryByContractId(contractId);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public Page<PaymentPlanVO> queryPage(PageParams<PaymentPlanPageQuery> params) {
        log.info("selectPaymentPlanList - 分页查询支付计划, list={}", params);
        R<Page<PaymentPlanVO>> result = paymentPlanFeign.queryPage(params);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public Page<PaymentPlanVO> listForContractManage(PageParams<PaymentPlanPageQuery> params) {
        Page<PaymentPlanVO> page = queryPage(params);
        page = ConvertUtil.convertPage(page, PaymentPlanVO.class);
        List<PaymentPlanVO> list = page.getRecords();
        if (list == null || list.isEmpty()) {
            return page;
        }
        List<Long> businessIds = list
                .stream()
                .map(PaymentPlanVO::getBusinessId)
                .collect(Collectors.toList());;
        List<ContractManage> contractManageList = contractManageService.listByIds(businessIds);
        if (contractManageList == null || contractManageList.isEmpty()) {
            return page;
        }
        // 将业务合同编号封装到支付计划中，认款需要使用合同编号
        Map<Long, ContractManage> contractManageMap = new HashMap<>(16);
        contractManageList.forEach(contractManage -> contractManageMap.put(contractManage.getId(), contractManage));
        list.forEach(paymentPlanVO -> {
            ContractManage contractManage = contractManageMap.get(paymentPlanVO.getBusinessId());
            if (contractManage != null) {
                paymentPlanVO.setBusinessNumber(contractManage.getContractNumber());
            }
        });
        return page;
    }

    @Override
    public List<PaymentPlanVO> queryByPlanPayTime(String startPlanPayTime, String endPlanPayTime) {
        log.info("queryByPlanPayTime - 计划付款时间范围查询未付款的付款计划, startPlanPayTime={}, endPlanPayTime={}", startPlanPayTime, endPlanPayTime);
        R<List<PaymentPlanVO>> result = paymentPlanFeign.queryByPlanPayTime(startPlanPayTime, endPlanPayTime);
        log.info("queryByPlanPayTime - 计划付款时间范围查询未付款的付款计划完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean saveOrUpdateBatch(List<PaymentPlanDTO> list) {
        log.info("saveOrUpdateBatch - 批量保存或更新付款计划, list={}", list);
        if (list == null || list.isEmpty()) {
            return false;
        }
        R<?> result = paymentPlanFeign.saveOrupdate(list);
        Boolean data = (Boolean) result.getData();
        if (!result.getIsSuccess() || !data) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return true;
    }

    @Override
    public boolean removePaymentPlanById(Long id) {
        log.info("removePaymentPlanById - 删除付款计划, list={}", id);
        R<?> result = paymentPlanFeign.delete(id);
        Boolean data = (Boolean) result.getData();
        if (!result.getIsSuccess() || !data) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return true;
    }
}
